﻿
using System;
using System.Collections.Generic;
using System.Text;

using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model;

namespace YouHP.Pages.WeightHistory
{
    public class HistoryEdit : YouHP.Pages.PatientBasePage
    {

        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            HandleCommon();
            if (ErrorCount > 0) return;

            if (IsPost) HandlePost();
            else HandleGet();
        }

        protected WeightHistoryInfo WeightData;
        protected new void HandleCommon()
        {
            this.AddLocation("体重记录", "/WeightHistory/Index.aspx");
            this.AddLocation("修改体重记录", "/WeightHistory/HistoryEdit.aspx");
            this.SetTitle("修改体重记录");

            int id = RequestTools.GetInt("id", 0);
            if (id <= 0) {
                this.AddErrLine("找不到体重信息");
                return;
            }
#if GENDATA
            WeightData.DataValue = 68;
            WeightData.RecordDate = Convert.ToDateTime("2007-03-02");
#else 
            WeightData = PatientBLL.Instance.GetWeightHistory(PrData.Id, id);
            if (WeightData.Id <= 0)
            {
                this.AddErrLine("找不到体重信息");
                return;
            }
#endif
        }

        protected new void HandlePost()
        {
            WeightHistoryInfo info = ValidateForm();
            if (info == null) return;
            bool succ = true;
#if GENDATA
            
#else
            succ = PatientBLL.Instance.UpdateWeightHistory(info);
#endif
            if (succ == true)
            {
                this.AddMsgLine("修改成功");
                this.Redirect("/WeightHistory/Index.aspx");
            }
            else 
            {
                this.AddErrLine("修改失败");
            }
        }

        protected WeightHistoryInfo ValidateForm()
        {
            WeightHistoryInfo info = WeightData;

            // 对时间进行判断
            string va = RequestTools.GetFormString("odate");
            if (va == "")
            {
                this.AddErrLine("请填写时间");
                return null;
            }

            DateTime dt;
            bool succ = DateTime.TryParse(va, out dt);
            if (succ == false)
            {
                this.AddErrLine("错误的时间 " + va);
                return null;
            }

            if (DateTime.Compare(dt, DateTime.Today) > 0)
            {
                this.AddErrLine("记录日期 " + dt.ToString("yyyy-MM-dd") + " 不能比当前日期 " + DateTime.Today.ToString("yyyy-MM-dd") + " 晚！");
                return null;
            }
            else if (DateTime.Compare(dt, PrData.StartDate) < 0)
            {
                this.AddErrLine("记录日期 " + dt.ToString("yyyy-MM-dd") + " 不能比第一次发病日期 " + PrData.StartDate.ToString("yyyy-MM-dd") + " 早！");
                return null;
            }
            
            // 检查时间是否重复
            if (WeightData.RecordDate.CompareTo(dt) != 0 && PatientBLL.Instance.CheckWeightHistory(PrData.Id, dt))
            {
                //提示是否需要覆盖历史记录
                this.AddErrLine("是否需要覆盖" + va + "已有的历史记录？");
                return null;
            }
            info.RecordDate = dt;

            double weight = RequestTools.GetFormDouble("wvalue", -1);
            if (weight <= 0)
            {
                this.AddErrLine("请填写有效的体重数据！");
                return null;
            }
            info.DataValue = weight;

            info.UpdateDate = DateTime.Now;

            return info;
        }

        protected new void HandleGet()
        {
            this.AddDatePicker();
        }
    }
}